import { Injectable } from '@nestjs/common';
import { JwtService } from '@nestjs/jwt';
import {User} from "@/entitys/base/user.entity";
import {classTransformerUtils} from "@/utils/classTransformerUtils";

@Injectable()
export class AuthService {
    constructor(private readonly jwtService: JwtService) {}

    // 创建jwt_token
    async createToken(userInfo: User) {
        const payload = classTransformerUtils.classToPlain(userInfo);
        return {
            access_token: this.jwtService.sign(payload)
        };
    }

    // 校验登录状态
    validateToken(token: string) {
        if (!token) {
            return false;
        }
        try {
            // const dd = this.jwtService.decode(token);
            // console.log(dd, "tokentoken");

            return this.jwtService.verify(token);
        } catch {
            return false;
        }
    }

    // 解码token
    decodeToken(token: string) {
        return this.jwtService.decode(token);
    }
}